home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Tool Chest / Dev.CD Feb 97 TC.toast / Sample Code / Toolbox / Fragment Tool / Sources / MenusStuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-20  |  12.1 KB  |  603 lines  |  [TEXT/MPCC]

  1. /*
  2.     File:        Menus.c
  3.  
  4.     Contains:    Handles the application's menus
  5.  
  6.     Written by:    Chris White, Developer Technical Support
  7.     
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9.     
  10.     Change History (most recent first):
  11.     
  12.                   9/28/95    CW        First release
  13.  
  14. */
  15.  
  16. #ifndef __MENUS__
  17.     #include <Menus.h>
  18. #endif
  19.  
  20. #ifndef __WINDOWS__
  21.     #include <Windows.h>
  22. #endif
  23.  
  24. #ifndef __DIALOGS__
  25.     #include <Dialogs.h>
  26. #endif
  27.  
  28. #ifndef __TEXTUTILS__
  29.     #include <TextUtils.h>
  30. #endif
  31.  
  32. #ifndef __DESK__
  33.     #include <Desk.h>
  34. #endif
  35.  
  36. #include <CodeFragments.h>
  37.  
  38.  
  39.  
  40.  
  41.  
  42. #ifndef __MENUSTUFF__
  43.     #include "MenuStuff.h"
  44. #endif
  45.  
  46. #include "DialogStuff.h"
  47.  
  48. #ifndef __STREAMS__
  49.     #include "Streams.h"
  50. #endif
  51.  
  52. #ifndef __UTILITIES__
  53.     #include "Utilities.h"
  54. #endif
  55.  
  56. #ifndef __PROTOTYPES__
  57.     #include "Prototypes.h"
  58. #endif
  59.  
  60.  
  61.  
  62.  
  63.  
  64. static Boolean EnableFileCommands ( WindowRef frontWindow );
  65. static Boolean EnableEditCommands ( WindowRef frontWindow );
  66. static Boolean EnableFragmentCommands ( WindowRef frontWindow );
  67. static void SetItemEnable ( MenuHandle theMenu, int16 theItem, Boolean bEnabled );
  68. static void DoAppleCmds ( int16 theItem );
  69. static void DoFileCmds ( int16 theItem );
  70. static void DoFragmentCmds ( int16 theItem );
  71. static void DoGetInfo ( WindowRef theWindow );
  72. static void DoDisplayExports ( WindowRef theWindow );
  73. static void DoMoveFragments ( WindowRef theWindow );
  74. static void DoCopyFragments ( WindowRef theWindow );
  75. static void DoRemoveFragments ( WindowRef theWindow );
  76. static OSErr PackageWindowData ( WindowRef theWindow, Ptr* theData );
  77.  
  78.  
  79.  
  80.  
  81. void MenuDispatch ( int32 menuResult )
  82. {
  83.     int16     theMenu = (menuResult >> 16);                    // menu selected
  84.     int16     theItem = (menuResult & 0x0000FFFF);               // item selected
  85.     
  86.     switch (theMenu)
  87.     {
  88.         case kAppleMenu: 
  89.             DoAppleCmds ( theItem );
  90.         break;
  91.         
  92.         case kFileMenu: 
  93.             DoFileCmds ( theItem );
  94.         break;
  95.         
  96.         case kEditMenu: 
  97.             SystemEdit ( theItem - 1 );
  98.         break;
  99.         
  100.         case kFragmentMenu:
  101.             DoFragmentCmds ( theItem );
  102.         break;
  103.     }
  104.     
  105.     HiliteMenu ( 0 );               // un-hilite selected menu
  106.     
  107.     return;
  108.     
  109. } // MenuDispatch
  110.  
  111.  
  112.  
  113. void AdjustMenus ( void )
  114. {
  115.     Boolean        bRedraw = false;
  116.     WindowRef    frontWindow;
  117.     
  118.     frontWindow = FrontWindow ( );
  119.     if ( EnableFileCommands ( frontWindow ) )
  120.         bRedraw = true;
  121.     if ( EnableEditCommands ( frontWindow ) )
  122.         bRedraw = true;
  123.     if ( EnableFragmentCommands ( frontWindow ) )
  124.         bRedraw = true;
  125.     
  126.     if ( bRedraw )
  127.         DrawMenuBar ( );
  128.     
  129.     return;
  130.     
  131. } // AdjustMenus
  132.  
  133.  
  134.  
  135. static Boolean EnableFileCommands ( WindowRef frontWindow )
  136. {
  137.     static Boolean    bIsEnabled = true;
  138.     Boolean            bHasDialog = false;
  139.     Boolean            bUpdate = false;
  140.     MenuHandle    theMenu = GetMenuHandle ( kFileMenu );
  141.     
  142.     
  143.     bHasDialog = frontWindow && GetWindowKind ( frontWindow ) == dialogKind;
  144.     
  145.     if ( bIsEnabled == bHasDialog )        // Or bIsEnabled != !bHasDialog
  146.     {
  147.         SetItemEnable ( theMenu, 0, !bHasDialog );
  148.         bIsEnabled = !bHasDialog;
  149.         bUpdate = true;
  150.     }
  151.     
  152.     SetItemEnable ( theMenu, cNew, !bHasDialog );
  153.     SetItemEnable ( theMenu, cOpen, !bHasDialog );
  154.     SetItemEnable ( theMenu, cClose, frontWindow && GetWindowGoAwayFlag ( frontWindow ) );
  155.     SetItemEnable ( theMenu, cSave, frontWindow && IsDocumentDirty ( frontWindow ) );
  156.     SetItemEnable ( theMenu, cSaveAs, frontWindow  && GetWindowType ( frontWindow ) == kDocumentWindowType );
  157.     SetItemEnable ( theMenu, cQuit, !bHasDialog );
  158.     
  159.     return bUpdate;
  160.     
  161. } // EnableFileCommands
  162.  
  163.  
  164.  
  165. static Boolean EnableEditCommands ( WindowRef frontWindow )
  166. {
  167.     static Boolean    bIsEnabled = true;
  168.     Boolean            bEnable = false;
  169.     MenuHandle        theMenu = GetMenuHandle ( kEditMenu );
  170.     
  171.     
  172.     bEnable = false;
  173.     if ( bIsEnabled != bEnable )
  174.     {
  175.         SetItemEnable ( theMenu, 0, bEnable );
  176.         bIsEnabled = bEnable;
  177.         
  178.         return true;
  179.     }
  180.     
  181.     return false;
  182.     
  183. } // EnableEditCommands
  184.  
  185.  
  186.  
  187. static Boolean EnableFragmentCommands ( WindowRef frontWindow )
  188. {
  189.     static Boolean    bIsEnabled = true;
  190.     Boolean            bHasSelection = false;
  191.     Boolean            bHasMultiSelection = false;
  192.     MenuHandle        theMenu = GetMenuHandle ( kFragmentMenu );
  193.     
  194.     
  195.     if ( frontWindow && GetWindowType ( frontWindow ) == kDocumentWindowType )
  196.     {
  197.         int16            theIndex = 0;
  198.         tWindowInfoPtr    theInfo;
  199.         
  200.         theInfo = (tWindowInfoPtr) GetWRefCon ( frontWindow );
  201.         if ( theInfo->listRef )
  202.         {
  203.             bHasSelection = GetSelection ( theInfo->listRef, &theIndex );
  204.             theIndex++;
  205.             bHasMultiSelection = GetSelection ( theInfo->listRef, &theIndex );
  206.         }
  207.     }
  208.     
  209.     if ( bIsEnabled != bHasSelection )
  210.     {
  211.         if ( bHasSelection )
  212.         {
  213.             Boolean    bHasMultiWindows;
  214.             
  215.             
  216.             // Always better to assign true or false so that if it's checked
  217.             // against the constants, it'll still work. If we simply cast the
  218.             // result of GetNextWindow to a boolean, a true value may not be
  219.             // exactly 1. If we then use “if ( bHasMultiWindows == true )”
  220.             // the evaluation will not be what we wanted. Further, because
  221.             // only 1 byte is assigned to the boolean, we could miss all the
  222.             // important bytes (those non-zero ones) so that bHasMultiWindows
  223.             // turned out to be false.
  224.             
  225.             bHasMultiWindows = (GetNextWindow ( frontWindow )) ? true : false;
  226.             
  227.             SetItemEnable ( theMenu, 0, true );
  228.             SetItemEnable ( theMenu, cGetInfo, !bHasMultiSelection );
  229.             SetItemEnable ( theMenu, cExports, !bHasMultiSelection );
  230.             SetItemEnable ( theMenu, cMoveTo, bHasMultiWindows );
  231.             SetItemEnable ( theMenu, cCopyTo, bHasMultiWindows );
  232.             SetItemEnable ( theMenu, cRemove, true );
  233.         }
  234.         else
  235.             SetItemEnable ( theMenu, 0, false );
  236.             
  237.         bIsEnabled = bHasSelection;
  238.         
  239.         return true;
  240.     }
  241.     
  242.     return false;
  243.     
  244. } // EnableFragmentCommands
  245.  
  246.  
  247.  
  248. static void SetItemEnable ( MenuHandle theMenu, int16 theItem, Boolean bEnabled )
  249. {
  250.     if ( theMenu )
  251.     {
  252.         if ( bEnabled )
  253.             EnableItem ( theMenu, theItem );
  254.         else
  255.             DisableItem ( theMenu, theItem );
  256.     }
  257.         
  258.     return;
  259.     
  260. } // SetItemEnable
  261.  
  262.  
  263.  
  264. static void DoAppleCmds ( int16 theItem )
  265. {
  266.     Str255    name;            // string for DA name
  267.     
  268.     
  269.     switch ( theItem )
  270.     {
  271.         case cAbout:
  272.             Alert ( kAboutDialog, nil );
  273.         break;
  274.         
  275.         default:
  276.             GetItem ( GetMHandle ( kAppleMenu ), theItem, (StringPtr) &name );
  277.             OpenDeskAcc ( (StringPtr) &name );
  278.         break;
  279.     }
  280. }
  281.  
  282.  
  283.  
  284. static void DoFileCmds ( int16 theItem )
  285. {    
  286.     switch ( theItem )
  287.     {
  288.         case cNew:
  289.             DoNewDocument ( );
  290.         break;
  291.         
  292.         case cOpen:
  293.             DoOpenDocument ( );
  294.         break;
  295.         
  296.         case cClose:
  297.             DoCloseDocument ( FrontWindow ( ) );
  298.         break;
  299.         
  300.         case cSave:
  301.             DoSave ( FrontWindow ( ) );
  302.         break;
  303.         
  304.         case cSaveAs:
  305.             DoSaveAs ( FrontWindow ( ) );
  306.         break;
  307.         
  308.         case cQuit:
  309.                                                 // Tell the application immediatly. It
  310.             gQuit = true;                        // might need to know.
  311.                                                 
  312.             if ( DoCloseAllDocuments ( ) )        // Did the user cancel?
  313.                 gQuit = false;
  314.         break;
  315.     }
  316.     
  317.     return;
  318.     
  319. } // DoFileCmds
  320.  
  321.  
  322.  
  323. static void DoFragmentCmds ( int16 theItem )
  324. {
  325.     WindowRef frontWindow = FrontWindow ( );
  326.     
  327.     switch ( theItem )
  328.     {
  329.         case cGetInfo:
  330.             DoGetInfo ( frontWindow );
  331.         break;
  332.         
  333.         case cExports:
  334.             DoDisplayExports ( frontWindow );
  335.         break;
  336.         
  337.         case cMoveTo:
  338.             DoMoveFragments ( frontWindow );
  339.         break;
  340.         
  341.         case cCopyTo:
  342.             DoCopyFragments ( frontWindow );
  343.         break;
  344.         
  345.         case cRemove:
  346.             DoRemoveFragments ( frontWindow );
  347.         break;
  348.     }
  349.     
  350.     return;
  351.     
  352. } // DoFragmentCmds
  353.  
  354.  
  355.  
  356. static void DoGetInfo ( WindowRef theWindow )
  357. {
  358.     int16        theIndex = 0;
  359.     ListRef        theList;
  360.     
  361.     theList = GetWListRef ( theWindow );
  362.     if ( GetSelection ( theList, &theIndex ) )
  363.     {
  364.         OSErr                    theErr;
  365.         DialogRef                theDialog;
  366.         tItemPtr                theItem;
  367.         Str255                    theTitle;
  368.         
  369.         
  370.         theItem = GetNthWindowItem ( theWindow, theIndex );
  371.         
  372.         GetIndString ( theTitle, 1000, 5 );
  373.         ConcatPStr ( theTitle, theItem->name, sizeof ( Str255 ) );
  374.         
  375.         theErr = CreateInfoDialog ( &theDialog, theTitle, theWindow, theIndex );
  376.         if ( theErr )
  377.             AlertUser ( kGenericErrorStr, theErr, nil );
  378.     }
  379.     
  380.     
  381.     return;
  382. }
  383.  
  384.  
  385.  
  386. static void DoDisplayExports ( WindowRef theWindow )
  387. {
  388.     int16        theIndex = 0;
  389.     ListRef        theList;
  390.     
  391.     theList = GetWListRef ( theWindow );
  392.     if ( GetSelection ( theList, &theIndex ) )
  393.     {
  394.         OSErr                    theErr;
  395.         DialogRef                theDialog;
  396.         tItemPtr                theItem;
  397.         Str255                    theTitle;
  398.         tWindowInfoPtr            theInfo;
  399.         tAddFragmentExportsRec    theRec;
  400.         
  401.         
  402.         theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  403.         theItem = GetNthWindowItem ( theWindow, theIndex );
  404.         
  405.         GetIndString ( theTitle, 1000, 1 );
  406.         ConcatPStr ( theTitle, theItem->name, sizeof ( Str255 ) );
  407.         
  408.         theRec.bIn = false;
  409.         theRec.u.out.theSpecPtr = &theInfo->fileSpec;
  410.         theRec.u.out.theItem = theItem;
  411.         theErr = CreateListDialog ( &theDialog, kListDialogOkay, theTitle,
  412.                                     &AddFragmentExports, (void*) &theRec );
  413.         if ( theErr )
  414.         {
  415.             // If an error occured, the record may contain some useful information
  416.             
  417.             StringPtr    theString = nil;
  418.             
  419.             if ( theRec.bIn )
  420.                 theString = theRec.u.in.theErrorStr;
  421.             if ( theErr == fragLibNotFound )
  422.                 AlertUser ( kUnresolvedDependenciesStr, 0, theString );
  423.             else
  424.                 AlertUser ( kGenericErrorStr, theErr, nil );
  425.         }
  426.     }
  427.     
  428.     
  429.     return;
  430. }
  431.  
  432.  
  433.  
  434. static void DoMoveFragments ( WindowRef theWindow )
  435. {
  436.     OSErr            theErr;
  437.     DialogRef        theDialog;
  438.     tDialogInfo*    theInfo;
  439.     Ptr                theData;
  440.     Str255            theTitle, windowTitle;
  441.     
  442.     
  443.     GetWTitle ( theWindow, windowTitle );
  444.     GetIndString ( theTitle, 1000, 2 );
  445.     ConcatPStr ( theTitle, windowTitle, sizeof ( Str255 ) );
  446.     
  447.     theErr = CreateListDialog ( &theDialog, kListDialogOkayCancel, theTitle,
  448.                                 &AddDocuments, nil );
  449.     if ( theErr )
  450.     {
  451.         AlertUser ( kGenericErrorStr, theErr, nil );
  452.         return;
  453.     }
  454.     
  455.     SetWindowSubType ( theDialog, kMoveFragmentWindowSubType );
  456.     
  457.     
  458.     // Packages up the window reference and an array of index
  459.     // values (precceded by an element count).
  460.     theErr = PackageWindowData ( theWindow, &theData );
  461.     if ( theErr )
  462.     {
  463.         AlertUser ( kGenericErrorStr, theErr, nil );
  464.         return;
  465.     }
  466.     
  467.     theInfo = (tDialogInfo*) GetWRefCon ( theDialog );
  468.     theInfo->refCon = (int32) theData;
  469.     
  470.     return;
  471.     
  472. }
  473.  
  474.  
  475.  
  476. static void DoCopyFragments ( WindowRef theWindow )
  477. {    
  478.     OSErr            theErr;
  479.     DialogRef        theDialog;
  480.     tDialogInfo*    theInfo;
  481.     Ptr                theData;
  482.     Str255            theTitle, windowTitle;
  483.     
  484.     
  485.     
  486.     GetWTitle ( theWindow, windowTitle );
  487.     GetIndString ( theTitle, 1000, 3 );
  488.     ConcatPStr ( theTitle, windowTitle, sizeof ( Str255 ) );
  489.     
  490.     theErr = CreateListDialog ( &theDialog, kListDialogOkayCancel, theTitle,
  491.                                 &AddDocuments, nil );
  492.     if ( theErr )
  493.     {
  494.         AlertUser ( kGenericErrorStr, theErr, nil );
  495.         return;
  496.     }
  497.     
  498.     SetWindowSubType ( theDialog, kCopyFragmentWindowSubType );
  499.     
  500.     
  501.     // Packages up the window reference and an array of index
  502.     // values (precceded by an element count).
  503.     theErr = PackageWindowData ( theWindow, &theData );
  504.     if ( theErr )
  505.     {
  506.         AlertUser ( kGenericErrorStr, theErr, nil );
  507.         return;
  508.     }
  509.     
  510.     theInfo = (tDialogInfo*) GetWRefCon ( theDialog );
  511.     theInfo->refCon = (int32) theData;
  512.     
  513.     return;
  514.     
  515. }
  516.  
  517.  
  518.  
  519. static void DoRemoveFragments ( WindowRef theWindow )
  520. {
  521.     int16        theIndex = 0;
  522.     ListRef        theList;
  523.     
  524.     theList = GetWListRef ( theWindow );
  525.     while ( GetSelection ( theList, &theIndex ) )
  526.     {
  527.         DeleteWindowFragment ( theWindow, theIndex );
  528.         theIndex++;
  529.     }
  530.     
  531.     return;
  532. }
  533.  
  534.  
  535.  
  536. //
  537. // Our refCon field is used to encapsulate the information about
  538. // the fragments being copied. Namely, the window reference and
  539. // an array of index values (precceded by an element count). This
  540. // packages up the information using a data stream.
  541. //
  542. static OSErr PackageWindowData ( WindowRef theWindow, Ptr* theData )
  543. {
  544.     OSErr            theErr;
  545.     int16            theIndex = 0;
  546.     int                theCount = 0;
  547.     ListRef            theList;
  548.     tStreamRef        theStream;
  549.     tStreamCursor    countCursor;
  550.     
  551.     
  552.     
  553.     theErr = NewStream ( &theStream, 100 );
  554.     if ( theErr )
  555.         return theErr;
  556.     
  557.     theErr = SetStreamData ( theStream, (Ptr) &theWindow, sizeof ( WindowRef ) );
  558.     if ( theErr )
  559.     {
  560.         DisposeStream ( theStream );
  561.         return theErr;
  562.     }
  563.     countCursor = GetStreamCursor ( theStream );
  564.     theErr = SetStreamData ( theStream, nil, sizeof ( int ) );
  565.     if ( theErr )
  566.     {
  567.         DisposeStream ( theStream );
  568.         return theErr;
  569.     }
  570.     
  571.     theList = GetWListRef ( theWindow );
  572.     while ( GetSelection ( theList, &theIndex ) )
  573.     {
  574.         SetStreamData ( theStream, (Ptr) &theIndex, sizeof ( int16 ) );
  575.         theCount++;
  576.         theIndex++;
  577.     }
  578.     
  579.     theErr = SetStreamCursor ( theStream, countCursor );
  580.     if ( theErr )
  581.     {
  582.         DisposeStream ( theStream );
  583.         return theErr;
  584.     }
  585.     theErr = SetStreamData ( theStream, (Ptr) &theCount, sizeof ( int ) );
  586.     if ( theErr )
  587.     {
  588.         DisposeStream ( theStream );
  589.         return theErr;
  590.     }
  591.     theErr = CompactStream ( theStream );
  592.     if ( theErr )
  593.         return theErr;
  594.     
  595.     *theData = (Ptr) theStream;
  596.     
  597.     return noErr;
  598.     
  599. } // PackageWindowData
  600.  
  601.  
  602.  
  603.